package edu.uwlax.cs.oayonlinestore.client;

import java.util.ArrayList;
import java.util.Iterator;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

import edu.uwlax.cs.oayonlinestore.client.utils.ExceptionTool;
import edu.uwlax.cs.oayonlinestore.vo.OrderSummaryVO;

public class Order extends OrderSummaryVO implements AsyncCallback {

	public interface OrderChangeListener {
		public void orderChanged(Order order);
	}

	private static Order instance;

	public static Order get() {
		if (instance == null)
			instance = new Order();
		return instance;
	}

	private ArrayList listeners = new ArrayList();

	private Order() {
		super(0, 0.0f, 0.0f, 0.0f, 0.0f, User.get(), 0, "");
	}

	public void addChangeListener(OrderChangeListener l) {
		listeners.add(l);
	}

	public void onFailure(Throwable arg0) {
		Window.alert(ExceptionTool.extractUserMessage(arg0));
	}

	public void onSuccess(Object arg0) {
		OrderSummaryVO o = (OrderSummaryVO) arg0;
		copy(o);
		for (Iterator i = listeners.iterator(); i.hasNext();)
			((OrderChangeListener) i.next()).orderChanged(this);
	}

	public void removeChangeListener(OrderChangeListener l) {
		listeners.remove(l);
	}

	public void update() {
		OnlinestoreRService.Util.getInstance().getCurrentOrder(this);
	}

}
